import loginApi from '@/api/system/login'
import authorityApi from '@/api/system/auth'
import { notifyError } from '@/utils'
import { getToken, setToken, removeToken, setStorage, removeStorage } from '@/utils/auth'
import  * as stn from '../../constants/storeName'
import router from "../../router/index"

// 获取菜单 -- 已合并至loginApi.getUserInfoByToken
function getUserMenuList () {
  //debugger
  /*if (getStorage(stn.MENU_LIST)) {
    return
  }*/
  authorityApi.getUserMenuList({}).then(response => {
    if (response.status === 0) {
      setStorage(stn.MENU_LIST, response.data.menuList)
      setStorage(stn.MENUS_PERMISSION, response.data.menusPermission)
      //debugger
      //window.location.reload()
    } else {
      notifyError(response.msg)
    }
  })
}

const user = {
  state: {
    userInfo: null,
    userId: undefined,
    userRole: undefined, // 1.系统用户 2.医生用户 3.医检用户
    isRemind: 0,         // 是否需要消息提醒(0.否 1.是)
    unReadCount: 0,         // 未读消息总数
    token: getToken(),
    hasCollectorRole: 0, //是否拥有收样员的角色权限(0.否 1.是)
    name: '',
    roles: [], // 角色id集合
  },

  mutations: {
    SET_USER_INFO: (state, userInfo) => {
      state.userInfo = userInfo
    },
    SET_USERID: (state, userId) => {
      state.userId = userId
    },
    SET_USERROLE: (state, userRole) => {
      state.userRole = userRole
    },
    SET_UN_READ_COUNT: (state, unReadCount) => {
      state.unReadCount = unReadCount
    },
    SET_IS_REMIND: (state, isRemind) => {
      state.isRemind = isRemind
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    }
  },

  actions: {
    // 获取用户信息
    GetUserInfo ({ commit, state }) {
      return new Promise((resolve, reject) => {
        //debugger
        // 获取菜单信息
        //getUserMenuList();

        // 获取用户登录信息
        loginApi.getUserInfoByToken(state.token).then(response => {
          //debugger
          const data = response.data
          let userInfo = {
            userId: data.userId,
            userName: data.userName,
            userRole:data.userRole,
            isRemind:data.isRemind,
            roles:data.roleIds
          }
          setStorage(stn.LOGIN_STATUS, true)
          setStorage(stn.USER_INFO, userInfo)
          setStorage(stn.USER_RELATION, data.userRelation)
          setStorage(stn.MENU_LIST, data.menuList)
          setStorage(stn.MENUS_PERMISSION, data.menusPermission)
          commit('SET_USER_INFO', userInfo)
          commit('SET_USERID', userInfo.userId)
          commit('SET_NAME', userInfo.userName)
          commit('SET_USERROLE', userInfo.userRole)
          commit('SET_IS_REMIND', userInfo.isRemind)
          if (userInfo.roles && userInfo.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', userInfo.roles)
          } else {
            reject('找不到系统用户,请联系您的系统管理员进行配置🛠️!')
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    LogOut ({ commit, state }) {
      return new Promise((resolve, reject) => {
        loginApi.sysUserLoginOut(state.token).then(response => {
          removeStorage(stn.LOGIN_STATUS);
          removeStorage(stn.USER_INFO);
          removeStorage(stn.USER_RELATION);
          removeStorage(stn.MENU_LIST);
          removeStorage(stn.MENUS_PERMISSION);
          commit('SET_USER_INFO', {});
          commit('SET_TOKEN', '')
          removeToken()
          //router.replace('/login')
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    FedLogOut ({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },
    setToken ({ commit }, token) {
      commit('SET_TOKEN', token)
      // 存入cookies
      setToken(token)
    },
    setUnReadCount ({ commit }, unReadCount) {
      commit('SET_UN_READ_COUNT', unReadCount)
    }
  }
}

export default user
